/*! \file bar.cpp
	
	This contains the implementation of the Bar class
*/

#include "bar.h"

Bar::Bar( Component* parent ) :
	Component( parent ),
	percentFilled( 0.0f ),
	highColor( 0.0f, 1.0f ),
	vertical( false ),
	displayPercent(false)
	{
	}

	
void Bar::setColors( Color lowColor, Color highColor ) {
	this->lowColor = lowColor;
	this->highColor = highColor;
	
}

void Bar::setPercentageFilled( float p ) {
	percentFilled = p;
	if ( percentFilled < 0.0f ) percentFilled = 0.0f;
	if ( percentFilled > 1.0f ) percentFilled = 1.0f;
}


void Bar::addPercentageFilled( float a ) {
	percentFilled += a;
	if ( percentFilled < 0.0f ) percentFilled = 0.0f;
	if ( percentFilled > 1.0f ) percentFilled = 1.0f;
}
	

void Bar::setOrientation( bool vertical ) {
	this->vertical = vertical;
}

float Bar::getPercentageFilled() {
	return percentFilled;
	
}

//~ void Bar::setWidth( int newW ) {
	//~ setSize( newW, h );
//~ }
	
//~ void Bar::setHeight( int newH ) {
	//~ setSize( w, newH );
//~ }
	
//~ void Bar::setSize( int newW, int newH ) {
	
//~ }
	
void Bar::draw() {
	if ( !visible ) {
		return;
	}
	
	Font* font = GUI::getInstance()->getFont();
	int screenX = getScreenX(), screenY = getScreenY();
	
	// Draw the background
	GUI::getInstance()->drawRectangle( w, h, bgColorTop, bgColorBottom, vertical );
	
	// Then draw the actual bar (draw the entire thing but clip down to the percentage filled)
	if ( vertical ) {
		GUI::setClippingArea( screenX, screenY, w, (int)(h * percentFilled) );
	} else {
		GUI::setClippingArea( screenX, screenY, (int)(w * percentFilled), h );
	}
	GUI::getInstance()->drawRectangle( w, h, lowColor, highColor, vertical );
	GUI::removeClippingArea();
	
	
	
	if ( border ) {
		ImageHandler::getInstance()->guiBorder( sideID1, cornerID1, screenX, screenY, w, h );
	}
	
	// Do this on top of the border if you have to
	if ( displayPercent ) {
		glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
		stringstream ss (stringstream::in | stringstream::out);
		ss << (int)(percentFilled * 100);
		string s;
		ss >> s;
		s += "%";
		font->writeString( s, w / 2 - font->widthOfString( s ) / 2, font->getMaxAdvanceY() / 2 );	
	}
	
}


void Bar::setDisplayPercent( bool show ) {
	displayPercent = show;
}
